#include "IRcom.h"

uint8_t encodeByte( uint8_t inByte )
{
	inByte = (0x40 & (inByte << 3)) | (0x10 & (inByte << 2)) | (0x04 & (inByte << 1)) | (0x01 & inByte);
	return (inByte & 0x55) | ((~((inByte & 0x55) << 1)) & 0xaa);
}

uint8_t decodeByte( uint8_t inByte )
{
	return ((inByte & 0x40) >> 3) | ((inByte & 0x10) >> 2) | ((inByte & 0x04) >> 1) | (inByte & 0x01);
}


void ir_com_init()
{
	// Initialize carrier waves
	PORTF.DIRSET = PIN0_bm | PIN1_bm | PIN4_bm | PIN5_bm | PIN6_bm | PIN7_bm;
	
	TCF2.CTRLA |= TC_CLKSEL_DIV4_gc;		
	TCF2.CTRLB = 0b11110011;				// Set outputs on pins 0,1,4,5,6, and 7
	TCF2.CTRLE = TC_BYTEM_SPLITMODE_gc;
	TCF2.HPER = 211;						// 32MHz / 4 * 211 = 38kHz
	TCF2.LPER = 211;
	TCF2.HCMPA = TCF2.HCMPB = TCF2.LCMPC = TCF2.LCMPD = 109;
	TCF2.LCMPA = TCF2.LCMPB = 109;
	
	// Initialize UARTs
	PORTC.DIRSET = PIN3_bm | PIN7_bm;		// TX pins as outputs
	PORTD.DIRSET = PIN3_bm;
	PORTE.DIRSET = PIN3_bm | PIN7_bm;
	PORTF.DIRSET = PIN3_bm;	
	
	PORTC.DIRCLR = PIN2_bm | PIN6_bm;		// RX pins as inputs
	PORTD.DIRCLR = PIN2_bm;
	PORTE.DIRCLR = PIN2_bm | PIN6_bm;
	PORTF.DIRCLR = PIN2_bm;
	
/*	USARTC0.BAUDCTRLB = 832 >> 4; USARTC0.BAUDCTRLA = 832 & 0xFF;		// Set baud rate 2400
	USARTC1.BAUDCTRLB = 832 >> 4; USARTC1.BAUDCTRLA = 832 & 0xFF;		// BSCALE = 0, BSEL = 832 @ 32MHz
	USARTD0.BAUDCTRLB = 832 >> 4; USARTD0.BAUDCTRLA = 832 & 0xFF;
	USARTE0.BAUDCTRLB = 832 >> 4; USARTE0.BAUDCTRLA = 832 & 0xFF;
	USARTE1.BAUDCTRLB = 832 >> 4; USARTE1.BAUDCTRLA = 832 & 0xFF;
	USARTF0.BAUDCTRLB = 832 >> 4; USARTF0.BAUDCTRLA = 832 & 0xFF;
*/	
	USARTC0.BAUDCTRLB = 0b11101101; USARTC0.BAUDCTRLA = 0;
	USARTC1.BAUDCTRLB = 0b11101101; USARTC1.BAUDCTRLA = 0;
	USARTD0.BAUDCTRLB = 0b11101101; USARTD0.BAUDCTRLA = 0;
	USARTE0.BAUDCTRLB = 0b11101101; USARTE0.BAUDCTRLA = 0;
	USARTE1.BAUDCTRLB = 0b11101101; USARTE1.BAUDCTRLA = 0;
	USARTF0.BAUDCTRLB = 0b11101101; USARTF0.BAUDCTRLA = 0;

	USARTC0.CTRLC = USART_CHSIZE_8BIT_gc | USART_PMODE_DISABLED_gc;		// 8 bits, no parity
	USARTC1.CTRLC = USART_CHSIZE_8BIT_gc | USART_PMODE_DISABLED_gc;
	USARTD0.CTRLC = USART_CHSIZE_8BIT_gc | USART_PMODE_DISABLED_gc;
	USARTE0.CTRLC = USART_CHSIZE_8BIT_gc | USART_PMODE_DISABLED_gc;
	USARTE1.CTRLC = USART_CHSIZE_8BIT_gc | USART_PMODE_DISABLED_gc;
	USARTF0.CTRLC = USART_CHSIZE_8BIT_gc | USART_PMODE_DISABLED_gc;
	
	PORTC.PIN3CTRL = PORT_INVEN_bm;										// Invert the output pins
	PORTC.PIN7CTRL = PORT_INVEN_bm;
	PORTD.PIN3CTRL = PORT_INVEN_bm;
	PORTE.PIN3CTRL = PORT_INVEN_bm;
	PORTE.PIN7CTRL = PORT_INVEN_bm;
	PORTF.PIN3CTRL = PORT_INVEN_bm;

	USARTC0.CTRLB = USART_RXEN_bm | USART_TXEN_bm;						// Enable communication
	USARTC1.CTRLB = USART_RXEN_bm | USART_TXEN_bm;
	USARTD0.CTRLB = USART_RXEN_bm | USART_TXEN_bm;
	USARTE0.CTRLB = USART_RXEN_bm | USART_TXEN_bm;
	USARTE1.CTRLB = USART_RXEN_bm | USART_TXEN_bm;
	USARTF0.CTRLB = USART_RXEN_bm | USART_TXEN_bm;
}

void varpot_init()
{
	
}

void set_ir_power(uint8_t dir, uint8_t power)
{
}

uint8_t get_ir_power(uint8_t dir)
{
}

/*
//  Receive complete interrupt service routine.
ISR( USARTC1_RXC_vect )
{
    USART_RXComplete( &USART_data );
}

//  Transmit complete interrupt servies routine
ISR( USARTC1_TXC_vect )
{
//	LEDPORT.OUTSET = 0xf0;
}


//  Data register empty  interrupt service routine.
ISR( USARTC1_DRE_vect )
{
    USART_DataRegEmpty( &USART_data );
}

*/
